package com.erp.warehouse.clouderp_warehouse.service.impl.allot;

import com.erp.warehouse.clouderp_warehouse.mapper.allot.AllotDetailMapper;
import com.erp.warehouse.clouderp_warehouse.mapper.allot.AllotMapper;
import com.erp.warehouse.clouderp_warehouse.pojo.allot.Allot;
import com.erp.warehouse.clouderp_warehouse.pojo.allot.AllotCreate;
import com.erp.warehouse.clouderp_warehouse.pojo.allot.AllotDetail;
import com.erp.warehouse.clouderp_warehouse.pojo.allot.AllotModify;
import com.erp.warehouse.clouderp_warehouse.service.allot.AllotService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 2025/11/10 8:48
 * author: 向远洋
 */
@Service
@Transactional
public class AllotServiceImpl implements AllotService {
    @Autowired
    AllotMapper allotMapper;
    @Autowired
    AllotDetailMapper allotDetailMapper;

    @Override
    public PageInfo<Allot> selectByPage(Map<String, Object> params) {
        int pageNum = Integer.parseInt(params.get("pageNum").toString());
        int pageSize = Integer.parseInt(params.get("pageSize").toString());
        PageHelper.startPage(pageNum, pageSize);
        List<Allot> list = allotMapper.selectAllotList(params);
        return new PageInfo<>(list);
    }

    @Override
    public void createWithItems(AllotCreate request) {
        // 1. 插入主单
        Allot allot = request.getAllot();
        allot.setId(UUID.randomUUID().toString());
        allot.setCreatedTime(new Date());
        List<AllotDetail> items = request.getDetailList();
        allotMapper.insert(allot);
        //插入明细
        for (AllotDetail item : items) {
            item.setId(UUID.randomUUID().toString());
            item.setAllotId(allot.getId());
        }
        if (items != null && !items.isEmpty()) allotDetailMapper.insertAllotDetailList(items);
    }

    @Override
    public AllotCreate getAllotWithDetail(String id) {
        Allot allot = allotMapper.selectAllot(id);
        List<AllotDetail> list = allotDetailMapper.selectAllotDetailListByAllotId(id);
        return new AllotCreate(allot, list);
    }

    @Override
    public void modifyAllotWithDetail(AllotModify allotModify) {
        //执行删除操作
        List<String> delList = allotModify.getDelList();
        if(!delList.isEmpty()) {
            int count = allotDetailMapper.deleteAllotDetailList(delList);
            if(count != delList.size()) throw new RuntimeException("删除调拨明细失败！");
        }
        //修改操作
        List<AllotDetail> modList = allotModify.getModList();
        if(!modList.isEmpty()) {
            for (AllotDetail allotDetail : modList) {
                int count = allotDetailMapper.updateAllotDetail(allotDetail);
                if(count != 1) throw new RuntimeException("修改调拨明细失败！");
            }
        }
        //新增操作
        List<AllotDetail> addList = allotModify.getAddList();
        for (AllotDetail allotDetail : addList) {
            allotDetail.setId(UUID.randomUUID().toString());
        }
        if(!addList.isEmpty()) allotDetailMapper.insertAllotDetailList(addList);

        //判断是否需要修改入库单
        boolean modified = allotModify.getOrderModified();
        if(modified) {
            allotMapper.updateAllot(allotModify.getAllot());
        }
    }

    /**
     * 自动生成入库出库单
     * @param allotCreate
     */
    @Override
    public void auditorAllotWithDetail(AllotCreate allotCreate) {
        //生成入库单
    }
}
